#include "util/crc32.hxx"
#include <string.h>
#include <assert.h>
#include <iostream>


uint32_t getCRC(const char* buf, int offset, int len) {

    CRC32 crc;
    crc.update(buf, offset, len);
    return crc.getValue();
}

void testStandard() {
    // From rfc3720 section B.4.
    char buf[32];

    memset(buf, 0, sizeof(buf));
    assert(0x8a9136aa == getCRC(buf, 0, sizeof(buf)));

    memset(buf, 0xff, sizeof(buf));
    assert(0x62a8ab43 == getCRC(buf, 0, sizeof(buf)));

    for (int i = 0; i < 32; i++) {
        buf[i] = i;
    }
    assert(0x46dd794e == getCRC(buf, 0, sizeof(buf)));

    for (int i = 0; i < 32; i++) {
        buf[i] = 31 - i;
    }
    assert(0x113fdb5c == getCRC(buf, 0, sizeof(buf)));

    unsigned char data[48] = {
        0x01, 0xc0, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x14, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x04, 0x00,
        0x00, 0x00, 0x00, 0x14,
        0x00, 0x00, 0x00, 0x18,
        0x28, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x02, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
    };
    assert(0xd9963a56 == getCRC(reinterpret_cast<char*>(data), 0, sizeof(data)));
}

void test() {
    CRC32 crc;
    const char *buf = "Introduction on CRC calculations";
    uint32_t len = strlen(buf);
    uint32_t crcv1, crcv2;
    
    crc.update(buf, 0, len);
    crcv1 = crc.getValue();
    crc.reset();

    crc.update(buf, 0, 11);
    crc.update(buf, 11, strlen("Introduction on CRC calculations") - 11);
    crcv2 = crc.getValue();
    
    assert(crcv1 == crcv2);
}

int main() {
    CRC32::init();

    testStandard();
    test();
}

